package lib.self.ex.activity;

import androidx.fragment.app.Fragment;
import android.view.View;
import android.widget.LinearLayout;

import com.rd.PageIndicatorView;

import java.util.List;

import lib.self.R;
import lib.self.ex.instance.PagerAdapterInstance;
import lib.self.util.view.LayoutUtil;
import lib.self.views.CustomViewPager;

/**
 * description: 自动管理ViewPager 和 PagerIndicator
 * author: Steven Xiao
 * date: 2017/5/15
 */
public abstract class ViewPagerActivityEx extends ActivityEx {
    private LinearLayout mLayoutHeader;
    private CustomViewPager mViewPager;
    private PagerAdapterInstance mPagerAdapter;
    private PageIndicatorView mIndicatorView;

    @Override
    public int getContentViewId() {
        return R.layout.viewpager_for_activity;
    }

    @Override
    public void findViews() {
        mViewPager = findViewById(getViewPagerResId());
        mLayoutHeader = findViewById(R.id.activity_viewpager_header);

        if (mLayoutHeader != null) {
            View headerView = getHeaderView();
            if (headerView != null) {
                if (headerView.getLayoutParams() == null) {
                    mLayoutHeader.addView(headerView, LayoutUtil.getLinearParams(LayoutUtil.MATCH_PARENT, LayoutUtil.WRAP_CONTENT));
                } else {
                    mLayoutHeader.addView(headerView);
                }
            }
        }
    }

    @Override
    public void setViewsValue() {
        mViewPager.setAdapter(getPagerAdapter());

        mIndicatorView = initPageIndicatorView();
        if (mIndicatorView != null) {
            mIndicatorView.setViewPager(mViewPager);
        }
    }

    protected int getViewPagerResId() {
        return R.id.activity_viewpager;
    }

    public View getHeaderView() {
        return null;
    }

    protected PageIndicatorView initPageIndicatorView() {
        return null;
    }

    protected void setCurrentItem(int item) {
        mViewPager.setCurrentItem(item);
    }

    protected void setCurrentItem(int item, boolean smoothScroll) {
        mViewPager.setCurrentItem(item, smoothScroll);
    }

    protected void setOffscreenPageLimit(int limit) {
        mViewPager.setOffscreenPageLimit(limit);
    }

    /**
     * 设置是否可以滑动
     */
    protected void setPagerScrollable(boolean scrollable) {
        mViewPager.setScrollable(scrollable);
    }


    /**
     * 设置二级viewpager是否可以滑动
     */
    protected void setChildrenScrollable(boolean scrollable) {
        mViewPager.setChildrenScrollable(scrollable);
    }

    protected void add(Fragment fragment) {
        getPagerAdapter().add(fragment);
    }

    protected void add(Fragment... fragments) {
        for (Fragment fragment : fragments) {
            add(fragment);
        }
    }

    protected final PagerAdapterInstance getPagerAdapter() {
        if (mPagerAdapter == null) {
            mPagerAdapter = initPagerAdapter();
        }
        return mPagerAdapter;
    }

    protected PagerAdapterInstance initPagerAdapter() {
        return new PagerAdapterInstance(getSupportFragmentManager());
    }

    protected int getCount() {
        return getPagerAdapter().getCount();
    }

    protected List<Fragment> getData() {
        return getPagerAdapter().getData();
    }

    protected int getCurrentItem() {
        return mViewPager.getCurrentItem();
    }

    protected Fragment getItem(int position) {
        return getPagerAdapter().getItem(position);
    }

    protected void showHeaderView() {
        showView(mLayoutHeader);
    }

    protected void goneHeaderView() {
        goneView(mLayoutHeader);
    }

    protected CustomViewPager getViewPager() {
        return mViewPager;
    }

    protected void invalidate() {
        getPagerAdapter().notifyDataSetChanged();
    }
}
